package cn.gzsf.weibo.controller;

import cn.gzsf.weibo.mapper.CommentMapper;
import cn.gzsf.weibo.pojo.dto.CommentDTO;
import cn.gzsf.weibo.pojo.entity.Comment;
import cn.gzsf.weibo.pojo.vo.CommentVO;
import cn.gzsf.weibo.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;
    @RequestMapping("insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        UserVO user = (UserVO)session.getAttribute("user");
        if(user==null){
            return 2;
        }
        Comment comment=  new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(user.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return 1;
    }
    @RequestMapping("select")
    public List<CommentVO> select(int id){
        return commentMapper.selectByWeiboId(id);
    }
}
